package com.youlai.quarkus.system.controller;

import com.youlai.quarkus.system.common.anno.annotation.PreventDuplicateSubmit;
import com.youlai.quarkus.system.common.model.Option;
import com.youlai.quarkus.system.common.result.Result;
import com.youlai.quarkus.system.model.form.DeptForm;
import com.youlai.quarkus.system.model.query.DeptQuery;
import com.youlai.quarkus.system.model.vo.DeptVO;
import com.youlai.quarkus.system.service.SysDeptService;
import jakarta.validation.Valid;
import jakarta.ws.rs.*;
import lombok.RequiredArgsConstructor;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.parameters.Parameter;
import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;

import java.util.List;


/**
 * 部门控制器
 *
 * @author haoxr
 * @since 2020/11/6
 */
@Tag(name = "05.部门接口")
@Path("/api/v1/dept")
@RequiredArgsConstructor
public class SysDeptController {
    
    private final SysDeptService deptService;

    @Operation(summary = "获取部门列表")
    @GET
    public Result<List<DeptVO>> listDepartments(
            @FormParam("status") Integer status,
            @FormParam("keyWords") String keyWord
    ) {
        DeptQuery queryParams = new DeptQuery(keyWord, status);
        List<DeptVO> list = deptService.listDepartments(queryParams);
        return Result.success(list);
    }

    @Operation(summary = "获取部门下拉选项")
    @Path("/options")
    @GET
    public Result<List<Option>> listDeptOptions() {
        List<Option> list = deptService.listDeptOptions();
        return Result.success(list);
    }

    @Operation(summary = "获取部门表单数据")
    @Path("/{deptId}/form")
    @GET
    public Result<DeptForm> getDeptForm(
            @Parameter(description ="部门ID") @PathParam("deptId") Long deptId
    ) {
        DeptForm deptForm = deptService.getDeptForm(deptId);
        return Result.success(deptForm);
    }

    @Operation(summary = "新增部门")
    @POST
    @PreventDuplicateSubmit
    public Result saveDept(
            @Valid @RequestBody DeptForm formData
    ) {
        Long id = deptService.saveDept(formData);
        return Result.success(id);
    }

    @Operation(summary = "修改部门")
    @Path(value = "/{deptId}")
    @PUT
    public Result updateDept(
            @PathParam("deptId") Long deptId,
            @Valid @RequestBody DeptForm formData
    ) {
        deptId = deptService.updateDept(deptId, formData);
        return Result.success(deptId);
    }

    @Operation(summary = "删除部门")
    @Path("/{ids}")
    @DELETE
    public Result deleteDepartments(
            @Parameter(description ="部门ID，多个以英文逗号(,)分割") @PathParam("ids") String ids
    ) {
        boolean result = deptService.deleteByIds(ids);
        return Result.judge(result);
    }

}
